package com.chaoyong.higo.activity;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.base.BaseActivity;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 个人资料-签名
 * 
 * @author zk
 *
 */
public class PersonalDataAutographActivity extends BaseActivity {

	private static final String ClassName = PersonalDataAutographActivity.class.getName();

	private LinearLayout base_left_iv;// 左侧返回按钮
	private TextView base_title_tv;// 标题
	private TextView base_right_tv;// 标题右侧文字

	private EditText person_input_qian_ming;
	private Button person_input_complete;

	@Override
	public int layoutResID() {
		return R.layout.activity_personal_autograph;
	}

	@Override
	public void findView() {
		findTitleIds();

		person_input_complete = V.f(this, R.id.person_input_complete);
		person_input_qian_ming = V.f(this, R.id.person_input_qian_ming);
		
		person_input_qian_ming.setFocusable(true);

		person_input_qian_ming.setFocusableInTouchMode(true);

		person_input_qian_ming.requestFocus();
		InputMethodManager imm = (InputMethodManager)getSystemService(this.INPUT_METHOD_SERVICE);
		imm.showSoftInputFromInputMethod(person_input_qian_ming.getWindowToken(), 0);
		//person_input_qian_ming.requestFocus();
	}

	private void findTitleIds() {
		base_left_iv = V.f(this, R.id.base_left_iv);
		base_title_tv = V.f(this, R.id.base_title_tv);
		base_right_tv = V.f(this, R.id.base_right_tv);

		base_title_tv.setText("修改签名");
		base_right_tv.setVisibility(View.GONE);
	}

	@Override
	public void init() {
		base_left_iv.setOnClickListener(this);
		person_input_complete.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		person_input_qian_ming.requestFocus();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.base_left_iv:// title左侧返回
			this.finish();
			break;
		case R.id.person_input_complete:// 完成
			// this.finish();
			httpNotifyName(PrefUtils.getString(PersonalDataAutographActivity.this, "userId", ""),
					getText(person_input_qian_ming));
			break;
		}
	}

	private String getText(EditText et) {
		return et.getText().toString().trim();
	}

	/**
	 * 修改签名
	 * 
	 * @param uid
	 *            用户id
	 * @param signature
	 *            签名
	 */
	private void httpNotifyName(String uid, String signature) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			JSONObject info = new JSONObject();
			jsonObject.putOpt("model", "MemberInfo");
			jsonObject.putOpt("function", "setAppMemberInfo");
			jsonObject.putOpt("info", info);

			info.put("signature", signature);
			info.put("uid", uid);
			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_appMemberInfo, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.i(ClassName, arg0.getMessage());
				showToast("修改失败");
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				JLog.i(ClassName, arg0.result);
				showToast("修改完成");
				PersonalDataAutographActivity.this.finish();
			}
		});
	}

}
